package com.seed.core.pojo.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author gchiaway
 * 日期: 2022-07-25
 * 时间: 10:52
 */
@Getter
@Setter
@ToString
public class SeedPageDTO<T> extends SeedMessageDTO {

    private Integer page = 1;

    private Integer size = 5;

    private Integer totalPage = 0;

    private Integer totalSize = 0;

    private List<T> list;

    public SeedPageDTO<T> success(List<T> list, Integer totalSize) {
        super.success();
        this.list = list;
        if (!ObjectUtils.isEmpty(totalSize)) {
            if (ObjectUtils.isEmpty(size) || size <= 0) {
                this.totalPage = totalSize % 5 == 0 ? totalSize / 5 : totalSize / 6;
            } else {
                this.totalSize = totalSize;
                this.totalPage = totalSize % size == 0 ? totalSize / size : totalSize / size + 1;
            }
        }
        return this;
    }

    public SeedPageDTO<T> success(List<T> list, Integer page, Integer size, Integer totalSize) {
        super.success();
        this.list = list;
        this.page = ObjectUtils.isEmpty(page) || page <= 0 ? 1 : page;
        this.size = ObjectUtils.isEmpty(size) || size <= 0 ? 5 : size;
        if (!ObjectUtils.isEmpty(totalSize)) {
            this.totalSize = totalSize;
            this.totalPage = totalSize % size == 0 ? totalSize / size : totalSize / size + 1;
        }
        return this;
    }

    public SeedPageDTO<T> success(String msg, Integer totalSize, List<T> list) {
        super.success(msg);
        this.list = list;
        if (!ObjectUtils.isEmpty(totalSize)) {
            if (ObjectUtils.isEmpty(size) || size <= 0) {
                this.totalPage = totalSize % 5 == 0 ? totalSize / 5 : totalSize / 6;
            } else {
                this.totalSize = totalSize;
                this.totalPage = totalSize % size == 0 ? totalSize / size : totalSize / size + 1;
            }
        }
        return this;
    }

    public SeedPageDTO<T> success(String msg, String code, Integer totalSize, List<T> list) {
        super.success(msg, code);
        this.list = list;
        if (!ObjectUtils.isEmpty(totalSize)) {
            if (ObjectUtils.isEmpty(size) || size <= 0) {
                this.totalPage = totalSize % 5 == 0 ? totalSize / 5 : totalSize / 6;
            } else {
                this.totalSize = totalSize;
                this.totalPage = totalSize % size == 0 ? totalSize / size : totalSize / size + 1;
            }
        }
        return this;
    }

    @Override
    public SeedPageDTO<T> error(String msg) {
        super.error(msg);
        return this;
    }

    @Override
    public SeedPageDTO<T> error(String msg, String code) {
        super.error(msg, code);
        return this;
    }

    public SeedPageDTO<T> error(List<T> list) {
        super.error();
        this.list = list;
        return this;
    }

    public SeedPageDTO<T> error(String msg, List<T> list) {
        super.error(msg);
        this.list = list;
        return this;
    }

    public SeedPageDTO<T> error(String msg, String code, List<T> list) {
        super.error(msg, code);
        this.list = list;
        return this;
    }
}
